﻿using System.Collections.Generic;
using System.Drawing;
using RaytracerLib.Layers;
using RaytracerLib.MathLib;
using RaytracerLib.Postprocessing;
using RaytracerLib.Postprocessing.LightCorrector;
using RaytracerLib.Resamplers;
using System;
using System.Linq;
using System.IO;

namespace RaytracerLib.Renderers
{
    public class Canvas
    {
        private ColorArrayFloat m_rendered_pixels;
        private ColorArrayFloat m_postprocessed_pixels;
        private Film m_film;

        public Canvas(Film a_film)
        {
            m_film = a_film;

            m_rendered_pixels = new ColorArrayFloat(a_film.Width,a_film.Height);
            m_postprocessed_pixels = new ColorArrayFloat(a_film.Width,a_film.Height);

            Rectangle dest_rect = new Rectangle(0, 0, a_film.Width, a_film.Height);
        }

        public ColorArrayFloat RenderedPixels
        {
            get
            {
                return m_rendered_pixels;
            }
        }

        public void PostprocessFinalize()
        {
            Rectangle rect = new Rectangle(0, 0, m_rendered_pixels.Width,
                m_rendered_pixels.Height);

            m_postprocessed_pixels.Draw(m_rendered_pixels, rect, rect);

            foreach (var pp in m_film.Postprocessors)
                pp.PostprocessFinalize(m_postprocessed_pixels);
        }

        public Bitmap GetPostprocessedBitmap(Rectangle a_rect)
        {
            return m_postprocessed_pixels.GetBitmap(a_rect);
        }

        public ColorArrayFloat GetPostprocessedArray(Rectangle a_rect)
        {
            return m_postprocessed_pixels.GetArrayRect(a_rect);
        }

        public Bitmap GetPostprocessedBitmap()
        {
            return m_postprocessed_pixels.GetBitmap();
        }

        public ColorArrayFloat PostprocessedArray
        {
            get
            {
                return m_postprocessed_pixels;
            }
        }

        public void Resample(Rectangle a_rect)
        {
            m_film.Resampler.Resample(a_rect, m_rendered_pixels);
        }

        public void Postprocess(Rectangle a_src_rect)
        {
            m_postprocessed_pixels.Draw(m_rendered_pixels, a_src_rect, a_src_rect);

            foreach (var pp in m_film.Postprocessors)
                pp.Postprocess(a_src_rect, m_postprocessed_pixels);
        }
    }
}